package com.arrogames.rgblabyrinth.game.graphics {
	import flash.display.BitmapData;
	import flash.display.Shape;
	
	import starling.display.Image;
	import starling.textures.Texture;
	
	public class Line extends Image {
		public function Line(color:uint, fromX:int, fromY:int, toX:int, toY:int) {
			
			var shape:Shape = new Shape();
			shape.graphics.lineStyle(1,color);
			//calculate difference between the to and the from
			var diffX:Number = toX - fromX;
			var diffY:Number = toY - fromY;
			var startX:Number = 0;
			var startY:Number = 0;
			//if the difference is negative, we want to change our from to the positive value and the to will be zero
			//this compensates for the change made to the from.
			if (diffX < 0) {
				startX = Math.abs(diffX);
				fromX = toX;
				toX = 0;
			} else {
				toX = diffX;
			}
			
			if (diffY < 0) {
				startY = Math.abs(diffY);
				fromY = toY;
				toY = 0;
			} else {
				toY = diffY;
			}
			//make sure we dont crash when the difference is only 0
			if(diffX == 0) {
				diffX = 1;
			}
			if(diffY == 0) {
				diffY = 1;
			}
			
			shape.graphics.moveTo(startX, startY);
			shape.graphics.lineTo(toX, toY);
			//when creating the bitmap, we need positive values
			var bitmapData:BitmapData = new BitmapData(Math.abs(diffX), Math.abs(diffY), true, 0);
			bitmapData.draw( shape, null, null, null, null, true );
			
			var texture:Texture = Texture.fromBitmapData(bitmapData, false, false, 1);
			
			super(texture);
			x = fromX;
			y = fromY;
		}
	}
}